package com.bci.pwtz.service.cad;


public class Cad_Device extends Cad_BaseDevice {
	public static final double byqtexth = 2.0D;
	public static final double drvtexth = 1.5D;
	public static final double tltexth = 1.0D;
	public boolean hasBX = false;
	public String layerName = "";
	public String deviceID = ""; //Added for DeviceID by liushikun on 2015.1.17
	public String deviceName = "";
	public int blockType = 0;
	public boolean otherdrv = false;
	public String parentDeviceID = "";
	public String parentDeviceName = "";
	public boolean isSameLine = true;
	public boolean isShow = true;
	public String showName = "";
	public String lineName = "";

	public static Cad_Point changPointRatio(double d1, double d2,
			Cad_Point point) {
		Cad_Point point1 = new Cad_Point();
		Cad_Point point2 = new Cad_Point();
		point1.copy(point);
		point2.x = (d1 * point1.x / d2);
		point2.y = (d1 * point1.y / d2);
		return point2;
	}

	public static String getMinSubName(String LongName, String shortName) {
		return shortName.replace(getSameQz(LongName, shortName), "");
	}

	public static String getSameQz(String LongName, String shortName) {
		if (LongName==null || LongName.length()<=0) {
			return "";
		}
		if (shortName==null || shortName.length()<=0) {
			return LongName;
		}
		
		int i = LongName.length();
		int j = shortName.length();
		int k = 0;
		int m = 0;
		int n = 0;
		while (true) {
			if ((k >= i) || (k >= j) || (n != 0)) {
				if (m <= 0)
					break;
				if (LongName.length() <= shortName.length())
					break;
				return LongName.substring(0, m + 1);
			}
			if (LongName.charAt(k) == shortName.charAt(k)) {
				if (LongName.charAt(k) > '')
					m = k;
				k++;
			} else {
				n = 1;
			}
		}
		return shortName.substring(0, m + 1);
	}

	public String Draw(boolean pBoolean) {
		String result = "";
		if (this.isShow) {
			String str1 = "";

			if ((pBoolean) || (this.blockType == Cad_BaseInfo.BLOCK_TYPE_TL)) { //BLOCK_TYPE_TL = 10; // 图例
				// setcur_layer = layer s %1$s
				Object[] arrayOfObject1 = new Object[1];
				arrayOfObject1[0] = this.layerName;
				str1 = String.format(Cad_BaseInfo.CAD_setcur_layer, arrayOfObject1);
			}
			result = str1;
			
			Object[] arrayOfObject2 = new Object[4];
			arrayOfObject2[0] = Cad_BaseInfo.block_names[this.blockType];
			arrayOfObject2[1] = Double.valueOf(this.linkPoint.x);
			arrayOfObject2[2] = Double.valueOf(this.linkPoint.y);
			arrayOfObject2[3] = Double.valueOf(0.0D);
			String str3 = String.format("insert %1$s %2$f,%3$f   %4$f\r\n", arrayOfObject2);
			result += str3;
			
			double d1 =0D;
			double d2 =0D;
			if (this.showName.length()<=0) {
				d1 = 1.5D;
				d2 = 0.5D;
				//public static final int BLOCK_TYPE_ZSBYQ = 3; // 柱上变压器
				//public static final int BLOCK_TYPE_PDBYQ = 4; // 配电箱式变压器
				//BLOCK_TYPE_TL = 10; // 图例
				if ((this.blockType != Cad_BaseInfo.BLOCK_TYPE_ZSBYQ) && (this.blockType != Cad_BaseInfo.BLOCK_TYPE_PDBYQ)) {
					if (this.blockType == Cad_BaseInfo.BLOCK_TYPE_TL) {
						d1 = 1.0D;
					}
				}
			}
			else{
				d1 = 2.0D;
				d2 = 2.2D;
			}
			double d3 = Cad_BaseInfo.getTextSize(this.showName, d1).cx;
//			Cad_Point point = new Cad_Point((this.drawRect.size().cx - d3) / 2.0D + this.drawRect.left, 
//					d2 + this.drawRect.top);
			Cad_Point point = new Cad_Point(linkPoint.x - d3/2,linkPoint.y+d2);
			if (this.blockType == Cad_BaseInfo.BLOCK_TYPE_TL){
				point.set(Cad_BaseInfo.tl_text_ofpt.x + this.drawRect.left,
						Cad_BaseInfo.tl_text_ofpt.y + this.drawRect.bottom);
			}
			// draw_text = text %1$f,%2$f %3$f 0 %4$s
			Object[] arrayOfObject3 = new Object[4];
			arrayOfObject3[0] = Double.valueOf(point.x);
			arrayOfObject3[1] = Double.valueOf(point.y);
			arrayOfObject3[2] = Double.valueOf(d1);
			arrayOfObject3[3] = this.showName;
			str3 = String.format(Cad_BaseInfo.CAD_draw_text, arrayOfObject3);
			result += str3;
		}
		return result;
	}

	public void changeRatio(double d1, double ratio) {
		Cad_Point point1 = new Cad_Point();
		Cad_Point point2 = new Cad_Point();
		point1.copy(this.linkPoint);
		point2.x = (d1 * point1.x / ratio);
		point2.y = (d1 * point1.y / ratio);
		moveof(point2.x - point1.x, point2.y - point1.y);
	}

	public void changeRatio(double d1, double d2,
			boolean pBoolean) {
		Cad_Point point1 = new Cad_Point();
		Cad_Point point2 = new Cad_Point();
		point1.x = (this.linkPoint.x - this.drawRect.pt1.x);
		point1.y = (this.linkPoint.y - this.drawRect.pt1.y);
		point2.x = (this.drawRect.pt2.x - this.linkPoint.x);
		point2.y = (this.drawRect.pt2.y - this.linkPoint.y);
		Cad_Point point3 = changPointRatio(d1, d2,	point1);
		Cad_Point point4 = changPointRatio(d1, d2,	point2);
		changeRatio(d1, d2);
		this.drawRect.pt1.x = (this.linkPoint.x - point3.x);
		this.drawRect.pt1.y = (this.linkPoint.y - point3.y);
		this.drawRect.pt2.x = (this.linkPoint.x + point4.x);
		this.drawRect.pt2.y = (this.linkPoint.y + point4.y);
	}

	public boolean equals(Object pObject) {
		if ((pObject instanceof Cad_Device)) {
			return ((Cad_Device) pObject).deviceName.equals(this.deviceName) || ((Cad_Device) pObject).deviceID.equals(this.deviceID);
		}
		return false;
	}

	public Cad_Point getlinkpt(Cad_Point point) {
		if (this.isShow) {
			if ((this.blockType == 4) || (this.blockType == 3))
				return this.linkPoint;
			return this.drawRect.getlinkpt(point);
		}
		return this.linkPoint;
	}

	public void moveof(double d1, double d2) {
		Cad_Point point = new Cad_Point();
		point.x = d1;
		point.y = d2;
		moveof(point);
	}

	public void moveof(Cad_Point point) {
		this.drawRect.moveof(point);
		Cad_Point point1 = this.linkPoint;
		point1.x += point.x;
		Cad_Point point2 = this.linkPoint;
		point2.y += point.y;
	}
    
	//设置CAD设备属性
	public boolean set(String deviceID, String deviceName, String parentDeviceID, String parentDevcieName,
			String lineName, boolean isPublicByq, int blockType,
			boolean isShow, boolean isSameLine,	Cad_Point point) {
		if (deviceID==null || deviceID.length()<=0) {
			return false;
		}
		if (deviceName==null || deviceName.length()<=0) {
			return false;
		}
		this.deviceID = deviceID;
		this.deviceName = deviceName;
		this.showName = getSameQz(deviceName, parentDevcieName);
		
		if (blockType==Cad_BaseInfo.BLOCK_TYPE_ZSBYQ || blockType==Cad_BaseInfo.BLOCK_TYPE_PDBYQ) {
			this.showName = getSameQz(deviceName, lineName);
		}
		else {
			this.showName = this.deviceName.replace(this.showName, "");
		}
		this.lineName = lineName;
		this.parentDeviceID = parentDeviceID;
		this.parentDeviceName = parentDevcieName;
		this.blockType = blockType;
		this.linkPoint.copy(point);
		Cad_BaseInfo.getDeviceRect(blockType, this.drawRect, point);
		this.isShow = isShow;
		if (this.blockType == Cad_BaseInfo.BLOCK_TYPE_DYGT_NZGT) {
			this.isShow = true;
		}
		this.isSameLine = isSameLine;
		if (this.blockType == Cad_BaseInfo.BLOCK_TYPE_DYDLFJX) {
			this.isSameLine = false;
		}

		switch (blockType) {
		case Cad_BaseInfo.BLOCK_TYPE_DYGT_ZXGT:  //0  直线杆塔
			//String[] arrayOfString2 = new String[3];
			//TranslatStr(deviceName, arrayOfString2);
			//this.showName = ("D" + arrayOfString2[1]);
			this.showName = ("D" + TranslatStr(deviceName));
			this.layerName = "直线杆塔";
			break;
		case Cad_BaseInfo.BLOCK_TYPE_DYGT_NZGT:  //1 耐张杆塔
			//String[] arrayOfString1 = new String[3];
			//TranslatStr(deviceName, arrayOfString1);
			//this.showName = ("N" + arrayOfString1[1]);
			this.showName = ("N" + TranslatStr(deviceName));
			this.layerName = "耐张杆塔";
			break;
		case Cad_BaseInfo.BLOCK_TYPE_DYDLJT:  //2  低压电缆接头
			this.layerName = "接头";
			this.isShow = false;
			break;
		case Cad_BaseInfo.BLOCK_TYPE_ZSBYQ:  //3 柱上变压器
			this.layerName = "专变";
			if (isPublicByq) {
				this.layerName = "公变";
			}
			break;
		case Cad_BaseInfo.BLOCK_TYPE_PDBYQ:  //4 专用变
			this.layerName = "专变";
			if (isPublicByq) {
				this.layerName = "公变";
			}
			break;
		case Cad_BaseInfo.BLOCK_TYPE_DYRSX:  //5 低压熔丝箱
			this.layerName = "熔丝箱";
			break;
		case Cad_BaseInfo.BLOCK_TYPE_DYSYDLBHQ: //6低压剩余电流保护器
			this.layerName = "剩余电流保护器"; 
			break;
		case Cad_BaseInfo.BLOCK_TYPE_DYPDX:  //7低压配电箱
			this.layerName = "配电箱";
			break;
		case Cad_BaseInfo.BLOCK_TYPE_DYDLFJX: //8 低压电缆分接箱
			this.layerName = "分接箱";
			break;
		case Cad_BaseInfo.BLOCK_TYPE_DYBX: //9 低压表箱
			this.layerName = "表箱";
			break;

		case Cad_BaseInfo.BLOCK_TYPE_TL: //10  图例
			this.showName = deviceName;
			this.layerName = "0";
			break;
		default:
			break;
		}
		return true;
	}
	
	/*
	 * 解析杆塔编号
	 * 从杆塔名中解析出几号杆，例如： 从 光华村一组公变1线05杆 解析出 05
	 */
	 public String TranslatStr(String deviceName)
	  {
		 if (deviceName==null || deviceName.length()<=0) {
			 return "";
		 }
		 
	     int i = deviceName.length();
	     int j = 0;
	     boolean flag = false;
	     String result =""; 

	    
	     for(j=i-1; j>=0; j--){
		    char c = deviceName.charAt(j);
		    
		    if ((c >= '0') && (c <= '9')) {
		    	if (!flag) {
		    		flag = true;
		    	}
		    	result = c + result;
		    }
		    else{
		    	if (flag) {
		    		break;
		    	}
		    }
	     }
	     return result;
	  }	
	/*
	 * 解析杆塔名
	 * 从杆塔名中解析出几号杆，例如： 从 光华村一组公变1线05杆 解析出 05杆
	 */
	 public void TranslatStr(String deviceName, String[] paramArrayOfString)
	  {
	    int i = deviceName.length();
	    paramArrayOfString[0] = "";
	    paramArrayOfString[1] = "";
	    paramArrayOfString[2] = "";
	    int j = i - 1;
	    if (j <= -1)
	      return;
	    
	    for(j=i-1; j>=0; j--){
		    char c = deviceName.charAt(j);
		    if ((c >= '0') && (c <= '9') && (paramArrayOfString[0].isEmpty())) {
		    	paramArrayOfString[1] = (c + paramArrayOfString[1]);
		    }
		    
		    if (paramArrayOfString[1].isEmpty()) {
		    	paramArrayOfString[2] = (c + paramArrayOfString[2]);
		    }
		    else{
		    	paramArrayOfString[0] = (c + paramArrayOfString[0]);
		    }
	    }
	    
//	    char c = deviceName.charAt(j);
//	    if ((c >= '0') && (c <= '9') && (paramArrayOfString[0].isEmpty()))
//	      paramArrayOfString[1] = (c + paramArrayOfString[1]);
//	    
//	    while (true)
//	    {
//	      j--;
//	      break;
//	      if (paramArrayOfString[1].isEmpty())
//	        paramArrayOfString[2] = (c + paramArrayOfString[2]);
//	      else
//	        paramArrayOfString[0] = (c + paramArrayOfString[0]);
//	    }
	  }

}
